package demo06

/**
 * @author xiaoZhao
 * @date 2022/7/19
 * @describe 规约和聚合
 *  根据传进来的操作进行规约聚合
 */
object Test15_HighLevelFunctiion_Reduce {
  def main(args: Array[String]): Unit = {
    val list = List(1, 2, 3, 4, 5)

    // reduce
    println(list.reduce(_ + _))
    println(list.reduceLeft(_ + _))

    val list2 = List(3, 4, 5, 7, 8, 80)
    println(list2.reduce(_ - _))
    println(list2.reduceLeft(_ - _))
    // 底层由递归实现
    println(list2.reduceRight(_ - _)) //3 - (4 - (5- (7- (8-90))))

    //fold
    println(list.fold(10)(_ - _)) // 10-1-2-3-4-5
    println(list.foldLeft(10)(_ - _))
    println(list.foldRight(11)(_ - _))


  }
}
